import wepy from 'wepy';

import * as OauthApi from './OauthApi'

/**
 * ZhongXiaoHuan
 * @description 新用户商品
 * @param {String} shopId 店铺序号
 */
export async function list(shopId) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 1) {
            times = arguments[1];
        }
        let result = await OauthApi.getWithClient(config.BASE_URL + "/api/v2/newuser/list", {
            shopId: shopId
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return searchDetail(shopId, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetCredentialsToken();
                    return searchDetail(shopId, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}